/**
    Dialog dialogs=new UtiDialogsFactory(ActBarcode.this);
    dialogs.CreateLoadingDialog();弹出
    dialogs.setDialogDismiss();销毁
 */

package com.onemile.utils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.onemile.app.R;
import com.onemile.app.UICommodityList_Select;
import com.onemile.controls.AdpLV_DialogMultiSelect;
import com.onemile.controls.AdpLV_DialogSingleSelect;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class UtiDialogsFactory
{
   // public UtiDialogsFactory(Context _context)
   // {
   // context = _context;
   // }
   //

   private static UtiDialogsFactory factory;

   public static UtiDialogsFactory getInstance()
   {
	  if (factory == null)
		 factory = new UtiDialogsFactory();
	  return factory;
   }

   private AlertDialog dialog;
   private AlertDialog.Builder builder;
   private ProgressDialog mDialog;

   private static final int loadingDialogNoTitle = 0;
   private static final int loadingDialogTitle = 1;

   // private static final int okAlertDialog=0;
   // private static final int okcancelAlertDialog=1;

   public interface DialogCallBack
   {
	  public void SingleSelectDialogItemClick(ListView listView, View currentView, int position);

	  public void MultiSelectDialogOKClick(ListView listView, ArrayList<Integer> selectList, int count);
   }

   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context, String title, String message)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
			dialog = null;
		 }
	  });
	  builder.create().show();
   }

   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context, String title, String message, DialogInterface.OnClickListener listener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", listener);
	  builder.create().show();
   }

   /**
    * 创建一个带有OK,Cancel按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    * @param OKButtonOnClickListener
    *           OK按钮的监听器
    */
   public void ShowOKCancelAlertDialog(Context context, String title, String message, OnClickListener OKButtonOnClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", OKButtonOnClickListener);
	  builder.setNegativeButton("取消", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {

			dialog.dismiss();
			dialog = null;
		 }
	  });
	  builder.create().show();
   }

   /**
    * 创建一个带两个自定义按钮的对话框
    * 
    * @param title
    *           标题
    * @param message
    *           正文内容
    * @param LeftBtnString
    *           左侧按钮文字
    * @param LeftBtnOnClickListener
    *           左侧按钮事件
    * @param RightBtnString
    *           右侧按钮文字
    * @param RightBtnOnClickListener
    *           右侧按钮事件
    */
   public void ShowCustomButtonAlertDialog(Context context, String title, String message, String LeftBtnString, OnClickListener LeftBtnOnClickListener, String RightBtnString,
		 OnClickListener RightBtnOnClickListener)

   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton(LeftBtnString, LeftBtnOnClickListener);
	  builder.setNegativeButton(RightBtnString, RightBtnOnClickListener);
	  builder.create().show();
   }

   public void ShowItemsDialog(Context context, String[] items, DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setItems(items, onClickListener);
	  builder.create().show();
   }

   public void ShowSingleSelectItemDialog(Context context, String[] items, int seletItem, DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setSingleChoiceItems(items, seletItem, onClickListener);
	  builder.create().show();
   }

   /**
    * 创建一个单选的对话框
    * 
    * @param context
    * @param list
    * @param title
    * @param callBack
    */
   public void ShowSingleSelectItemDialog(Context context, ArrayList<?> list, String title, final DialogCallBack callBack)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  LayoutInflater mInflater = LayoutInflater.from(context);
	  final ListView listView = (ListView) mInflater.inflate(R.layout.dialog_listview, null);
	  listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	  {
		 @Override
		 public void onItemClick(AdapterView<?> arg0, View currentView, int position, long arg3)
		 {
			callBack.SingleSelectDialogItemClick(listView, currentView, position);
			dialog.dismiss();
			dialog = null;
		 }
	  });
	  AdpLV_DialogSingleSelect adp = new AdpLV_DialogSingleSelect(list, context);
	  listView.setAdapter(adp);

	  if (title == null || title.equals(""))
	  {
		 dialog.setView(listView);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(listView);
	  }
	  dialog.show();
   }

   public void ShowMultiSelectItemDialog(Context context, final ArrayList<?> list, ArrayList<Integer> selectList, String title, final DialogCallBack callBack)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  LayoutInflater mInflater = LayoutInflater.from(context);
	  final ListView listView = (ListView) mInflater.inflate(R.layout.dialog_listview, null);

	  
	  listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
	  {
		 @Override
		 public void onItemClick(AdapterView<?> arg0, View currentView, int position, long arg3)
		 {
			//单击每条记录的时候，改变CheckBox的选中状态，并更新到HashMap中
			AdpLV_DialogMultiSelect adp_ = (AdpLV_DialogMultiSelect) listView.getAdapter();
			AdpLV_DialogMultiSelect.ViewHolder holder = (AdpLV_DialogMultiSelect.ViewHolder) currentView.getTag();
			adp_.SetSelect(position, !holder.ckbItem.isChecked());
			holder.ckbItem.setChecked(!holder.ckbItem.isChecked());
		 }
	  });
	  AdpLV_DialogMultiSelect adp = new AdpLV_DialogMultiSelect(list, context);
	  if (selectList != null)//如果不为空，则加载之前的选中状态
	  {
		 int len = selectList.size();
		 for (int i = 0; i < len; i++)
		 {
			adp.SetSelect(selectList.get(i), true);
		 }
	  }
	  listView.setAdapter(adp);

	  if (title == null || title.equals(""))
	  {
		 dialog.setView(listView);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(listView);
	  }
	  dialog.setButton("确定", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			ArrayList<Integer> result = new ArrayList<Integer>();
			AdpLV_DialogMultiSelect _adp = (AdpLV_DialogMultiSelect) listView.getAdapter();
			int size = _adp.getCount();
			for (int i = 0; i < size; i++)
			{
			   if (_adp.GetSelectState(i))
			   {
				  result.add(i);
			   }
			}
			int count = result.size();
			if(count==0)
			{
			   callBack.MultiSelectDialogOKClick(listView, null, count);
			}
			else 
			{
			   callBack.MultiSelectDialogOKClick(listView, result, count);
			}
			dialog.dismiss();
			dialog = null;
		 }
	  });
	  dialog.setButton2("取消", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
			dialog = null;
		 }
	  });
	  dialog.show();
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialog(Context context, String title, View view, int height, int width)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }

	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
	  dialog.show();
   }

   /**
    * 
    * @param context
    * @param title
    * @param view
    */
   public void ShowCustomViewDialog(Context context, String title, View view)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.show();
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialogWithOKCancel(Context context, String title, View view, int height, int width, DialogInterface.OnClickListener clickListener)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.setButton("确定", clickListener);
	  dialog.setButton2("取消", new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {

			dialog.dismiss();
			dialog = null;
		 }
	  });
	  dialog.show();
	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    */
   public void ShowCustomViewDialogWithOKCancel(Context context, String title, View view, DialogInterface.OnClickListener clickListener)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.setButton("确定", clickListener);
	  dialog.setButton2("取消", new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
			dialog = null;
		 }
	  });
	  dialog.show();
   }

   /**
    * 关闭警告对话框
    */
   public void DismissDialog()
   {
	  try
	  {
		 if (dialog!=null&dialog.isShowing())
		 {
			dialog.dismiss();
			dialog = null;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissAlertDialog()", e);
	  }

   }

   public void ShowToastInfoShort(Context context, String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
   }

   public void ShowToastInfoLong(Context context, String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_LONG).show();
   }

   /**
    * 关闭loading对话框
    */
   public void DismissLoadingDialog()
   {
	  try
	  {
		 if (mDialog!=null&&mDialog.isShowing())
		 {
			mDialog.dismiss();
			mDialog = null;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissLoadingDialog()", e);
	  }

   }

   /**
    * 创建一个没有标题栏的loading窗口
    * 
    * @param message
    *           信息
    */
   public void ShowLoadingDialogNoTitle(Context context, String message)
   {
	  CreateLoadingDialog(context, loadingDialogNoTitle, message, null);
   }

   public void ShowLoadingDialog(Context context, String message, String title)
   {
	  CreateLoadingDialog(context, loadingDialogTitle, message, title);
   }

   protected void CreateLoadingDialog(Context context, int id, String message, String title)
   {
	  mDialog = new ProgressDialog(context);
	  switch (id)
	  {
		 case loadingDialogTitle:
			mDialog.setTitle(title);
			mDialog.setMessage(message);
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			break;
		 case loadingDialogNoTitle:
			mDialog.setMessage(message);
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			break;
	  }
	  mDialog.show();
   }
}
